package com.jialanmo.zhihudaily.ui.main;

import com.jialanmo.zhihudaily.Application;
import com.jialanmo.zhihudaily.data.model.Themes;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public final class MainActivityPresenter implements MainContract.ActivityPresenter {

    private final MainContract.ActivityView mView;

    MainActivityPresenter(MainContract.ActivityView view) {
        mView = view;
    }

    @Override
    public void start() {
        getThemes();
    }

    @Override
    public void getThemes() {
        Application.getApplication(mView.getContext()).getZhihuApi()
                .getThemes()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Themes>() {
                    @Override
                    public void call(Themes themes) {
                        mView.showThemes(themes);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showMessage(throwable.getMessage());
                    }
                });
    }
}
